<?php
/**
 * Description: 辅助方法
 * User: devforma
 * Date: 16/6/6
 * Time: 14:30
 */

class VipUtils {

    /**
     * 获取作业状态文本
     * 
     * @param int $statusId 状态id
     * @return string
     */
    public static function getStatusText($statusId) {
        $status = [
            0 => '未完成',
            1 => '已完成',
            3 => '已提交',
            4 => '被驳回',
            5 => '过期完成',
            6 => '过期'
        ];

        return $status[$statusId];
    }

    /**
     * 作业信息
     *
     * @param $exerciseId
     * @return mixed
     */
    public static function getHistoryUserPrimaryExerciseInfo($exerciseId,$userId) {
        global $uddb;

        return $uddb->getRow("select * from history_user_primary_exercise where exercise_id=:eId and user_id=:uId",
            [':eId' => $exerciseId,':uId' => $userId]);

    }

    /**
     * 获取全站最快通关时间
     *
     * @param $queryArgs
     * @return int
     */
    public static function getFastest($queryArgs) {

        if (empty($queryArgs['type_id']) || empty($queryArgs['is_arr'])) {
            return null;
        }

        $cacheKey = 'vip:fastest_'.$queryArgs['type_id'].':';

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : 'default';

        $fastestArr = self::getUserDataGlobal($cacheKey);

        if (empty($fastestArr) || $fastestArr == null) {
            $fastestArr[$level] = !empty($queryArgs['duration']) ? $queryArgs['duration'] : null;
        }
        //返回值是否为数组
        return ($queryArgs['is_arr'] == 1) ? $fastestArr : $fastestArr[$level];
    }

    /**
     * 更新全站最快通关时间
     *
     * @param $queryArgs
     * @return int
     */
    public static function setFastest($queryArgs) {

        if (empty($queryArgs['type_id']) || empty($queryArgs['duration'])) {
            return false;
        }
        $cacheKey = 'vip:fastest_'.$queryArgs['type_id'].':';

        $queryArgs['is_arr'] = 1;

        $fastestArr = self::getUserDataGlobal($cacheKey);

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : 'default';

        $fastestArr[$level] = $queryArgs['duration'];

        self::setUserDataGlobal($cacheKey,$fastestArr);
    }

    /**
     * 获取个人最快通关时间
     *
     * @param $queryArgs
     * @return array|mix|mixed|null
     */
    public static function getFastestSelf($queryArgs) {

        if (empty($queryArgs['type_id']) || empty($queryArgs['user_id'])) {
            return null;
        }

        $cacheKey = 'vip:fastestSelf_'.$queryArgs['type_id'].'_'.$queryArgs['user_id'].':';

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : 'default';

        $fastestSelfArr = self::getUserDataGlobal($cacheKey);

        if (empty($fastestSelfArr) || $fastestSelfArr == null) {
            $fastestSelfArr[$level] = !empty($queryArgs['duration']) ? $queryArgs['duration'] : null;
        }
        //返回值是否为数组
        return ($queryArgs['is_arr'] == 1) ? $fastestSelfArr : $fastestSelfArr[$level];
    }

    /**
     * 更新个人最快通关时间
     *
     * @param $queryArgs
     * @return bool
     */
    public static function setFastestSelf($queryArgs) {

        if (empty($queryArgs['type_id']) || empty($queryArgs['user_id']) || empty($queryArgs['duration'])) {
            return false;
        }

        $cacheKey = 'vip:fastestSelf_'.$queryArgs['type_id'].'_'.$queryArgs['user_id'].':';

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : 'default';

        $fastestSelfArr = self::getUserDataGlobal($cacheKey);

        $fastestSelfArr[$level] = $queryArgs['duration'];

        self::setUserDataGlobal($cacheKey,$fastestSelfArr,$queryArgs['user_id']);
    }

    /**
     * 获取已完成作业的人数
     *
     * @return int
     */
    public static function getRanking($queryArgs) {

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : '';

        $cacheKey = 'vip:getRanking_'.$queryArgs['exercise_id'].'_'.$level.':';

        $num = self::getUserDataGlobal($cacheKey);

        if ($num == null) {
            $num = 0;
            self::setUserDataGlobal($cacheKey,$num);
        }
        return $num;
    }

    /**
     * 更新已完成作业的人数
     *
     * @param $queryArgs
     */
    public static function setRanking($queryArgs) {

        $level = !empty($queryArgs['level']) ? $queryArgs['level'] : '';

        $cacheKey = 'vip:getRanking_'.$queryArgs['exercise_id'].'_'.$level.':';

        $num = self::getRanking($queryArgs);
        $num++;

        self::setUserDataGlobal($cacheKey,$num);
    }

    /**
     * 获取value值
     *
     * @param $key
     * @return array|mixed
     */
    public static function getUserDataGlobal($key) {
        global $uddb;

        if (($value = cacheGet($key)) == null) {

            $value = $uddb->getVar("select meta_value from user_data_global where `meta_key`=:meta_key",[':meta_key' => $key]);

            cacheAdd($key, $value, 48*60*60);
        }

        return json_decode($value,true);

    }

    /**
     * 更新value值
     *
     * @param $key
     * @param $val
     */
    public static function setUserDataGlobal($key,$val,$userId = 0) {
        global $uddb;

        $exist = $uddb->getVar("select id from user_data_global where `meta_key`=:meta_key",[':meta_key' => $key]);

        if (!empty($exist)) {
            $data = [
                'meta_value'    => json_encode($val)
            ];
            $uddb->update('user_data_global', $data, ['id' => $exist]);

        } else {
            $data = [
                'user_id'       => $userId,
                'meta_key'      => $key,
                'meta_value'    => json_encode($val),
                'updated_time'  => date('Y-m-d H:i:s'),
                'created_time'  => date('Y-m-d H:i:s')
            ];
            $uddb->insert('user_data_global', $data);
        }

        cacheSet($key, json_encode($val), 48*60*60);
    }

    /**
     * 获取真题库真题
     *
     * @param $id
     * @return mixed
     */
    public static function getOriginalZtkQuestions ($id) {
        global $cmsdb;

        $result = $cmsdb->getRow("select question,material from original_ztk_questions where id = :Id",
            [':Id' => $id]);

        return $result;
    }
}